PYTHON h5py库包安装及读写 | 您所在的位置:网站首页 › python 怎么安装包 › PYTHON h5py库包安装及读写 |
一、h5py库包安装
1. 在线安装
基本方法:
cmd安装:pip install h5py pycharm环境:file-->setting-->PROJECT-->Project interpreter-->**+**-->搜索需要的库包-->installing 然而,本人在线安装该库包时报错,猜测可能原因是依赖环境下载问题,于是下载相应库包并手动安装: 2. 离线安装 2.1 登录PYPI网站下载所需库包网址:https://pypi.org/ 搜索h5py库包和相应的依赖库包six库包,下载对应python版本及个人计算机的库包 ATTENTION:在python中安装h5py库包需要的依赖条件:six库包,我们要同时下载该库包,先安装之 2.2 cmd安装下载的whl文件以windows 64位系统+python3.6环境为例: 下载了h5py-2.10.0-cp36-cp36m-win_amd64.whl和six-1.14.0-py2.py3-none-any.whl二库包 打开cmd窗口,转到下载的库包位置:cd D:\\支持环境软件\python包先安装依赖环境,输入命令pip install six-1.14.0-py2.py3-none-any.whl安装h5py库包,输入命令pip install h5py-2.10.0-cp36-cp36m-win_amd64.whlOK! 二、h5py基本操作 1. HDF/h5文件基本构成可以将h5文件理解成文件夹系统,group就是文件夹,dataset就是文件。 group里面可以存储group以及dataset。 2. 写——利用h5py库包创建h5文件及其group和datasetimport numpy as np import h5py 2.1 创建一个h5文件,文件指针是ff = h5py.File('HDF5_FILE.h5','a') 创建模式: r Readonly, file must exist,仅读,文件必须已经存在 r+ Read/write, file must exist,读写,文件必须已经存在 w Create file, truncate if exists,新建文件,如果存在则覆盖 w- or x Create file, fail if exists,新建文件,如果存在则新建失败 a Read/write if exists, create otherwise (default),如果文件存在则读写,如果不存在则覆盖 2.2 对创建的h5文件创建其group和dataset:假设我要创建3个group:Information、Data_group、Data_parameter g1 = f.create_group("Information") g2 = f.create_group("Data_group") g3 = f.create_group("Data_parameter") 在Information组里面创建数据集:a. 数据为numpy格式: Inf_Data = np.array([[67.5,1],[350,4],[6.70,4.9],[10,58.4],[12000,97]]) d1 = g1.create_dataset('Spectrum_Resolution', data=SpeReData) 添加attribute:d1.attrs['col1'] = 'Center Length' d1.attrs['unit'] = 'm' b. 数据为str数组: dt = h5py.special_dtype(vlen=str) # 计划创建的数据格式是str data = np.array([['slavery'],['kmi']]) d1 = g1.create_dataset('Slavery', data.shape, dtype=dt) d1[:] = data 3. 读——获取h5文件各group和dataset信息稍后发布 4. 批量读写 5. 整体代码 |
CopyRight 2018-2019 实验室设备网 版权所有 |